package cn.fanqinghong.product.service.impl;

import cn.fanqinghong.basic.util.BaiduAuditUtils;
import cn.fanqinghong.product.domain.Product;
import cn.fanqinghong.product.domain.ProductDetail;
import cn.fanqinghong.product.domain.ProductOnlineAuditLog;
import cn.fanqinghong.product.mapper.ProductDetailMapper;
import cn.fanqinghong.product.mapper.ProductMapper;
import cn.fanqinghong.product.mapper.ProductOnlineAuditLogMapper;
import cn.fanqinghong.product.service.IProductService;
import cn.fanqinghong.basic.service.impl.BaseServiceImpl;
import cn.fanqinghong.product.vo.ProductVo;
import cn.fanqinghong.user.domain.Logininfo;
import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fanqinghong
 * @since 2022-06-28
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;


    @Override
    public void removeById(Long id) {
        productMapper.deleteById(id);
        productDetailMapper.deleteByProductId(id);
    }

    /**
     * 服务上架
     * @param ids
     */
    @Override
    public void onSale(List<Long> ids) {
        //审核服务
        for (Long id : ids) {
            //1.通过id查询服务
            Product product = productMapper.loadById(id);
            //创建一个可变字符用来给装去审核不通过的内容
            StringBuilder sb = new StringBuilder();
            /*if (!BaiduAuditUtils.TextCensor(product.getName())) {
                sb.append(product.getName()+"含有敏感字符！！");
            }
            if (!BaiduAuditUtils.ImgCensor(product.getResources())){
                sb.append(product.getResources()+"照片审核不通过");
            }*/
            ProductOnlineAuditLog log = new ProductOnlineAuditLog();
            //如果审核不通过，可变字符串的长度是>1的
            if (sb.toString().length()>1){
                //纪律产品Id
                log.setProductId(id);
                //
                log.setNote(sb.toString());
                //记录审核不通过的日志
                productOnlineAuditLogMapper.save(log);
                //结束本次内容
                continue;
            }
            //审核通过修改状态码
            ProductVo productVo = new ProductVo();
            productVo.setId(id);
            productMapper.onSale(productVo);

        }

    }

    /**
     * 服务下架
     * @param ids
     */
    @Override
    public void offSale(List<Long> ids) {
        ProductVo productVo = new ProductVo();
        productVo.setIds(ids);
        productMapper.offSale(productVo);
    }

    /**
     * 修改
     * @param product
     */
    @Override
    public void update(Product product) {
        //通过id查询
        //获取到传入的详情
        ProductDetail detail = product.getDetail();
        productDetailMapper.update(detail);
        productMapper.update(product);


    }

    /**
     * 新增
     * @param product
     */
    @Override
    public void save(Product product) {
        //保存product的数据
        productMapper.save(product);
        //获取到详情
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        //保存详情
        productDetailMapper.save(detail);

    }

    @Override
    public Product queryById(Long id) {
        //通过productId查询详情
        ProductDetail detail = productDetailMapper.loadByProductId(id);
        Product product = productMapper.loadById(id);
        product.setDetail(detail);
        return product;
    }
}
